<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>新建车型项目</title>
    <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css" />
{#    <link rel="stylesheet" type="text/css" href="/static/css/admin.css" />#}
</head>
<body>
    <div class="wrap-container">
        <form class="layui-form" style="width: 90%;padding-top: 20px;" id="taskForm">

            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="task_name" required lay-verify="required|maxLength32" placeholder="新车型项目名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">编码</label>
                <div class="layui-input-block">
                    <input type="text" name="task_abbr" required lay-verify="required|maxLength8" placeholder="英文缩写" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">负责人</label>
                <div class="layui-input-block">
                    <input type="text" name="task_pm"  placeholder="负责人姓名 + 联系方式"  class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="task_status" required lay-verify="required" class="layui-input">
                        <option value="">请选择状态</option>
                        <option value="待启动">待启动</option>
                        <option value="分析中">分析中</option>
                        <option value="已发布">已发布</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="task_desc" placeholder="详细说明" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script src="/static/js/layui.js" type="text/javascript" charset="utf-8"></script>
    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;

            form.render();

            form.verify({
                maxLength32: function(value) {
                    if (value.length > 32) {
                        return '名称长度不能超过32个字符';
                    }
                },
                maxLength8: function(value) {
                    if (value.length > 8) {
                        return '缩写长度不能超过8个字符';
                    }
                }
            });

            form.on('submit(formDemo)', function(data) {
                layer.msg(JSON.stringify(data.field));
                // 使用fetch发送POST请求，确保只发送表单字段数据
                fetch('/add_task', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data.field) // 只发送表单字段数据
                })
                .then(response => response.json())
                .then(data => {
                    layer.msg(data.msg);
                    // 刷新父窗口的表格
                    parent.layui.table.reload('projectTable');
                    // 关闭当前页面
                    setTimeout(function() {
                        var index = parent.layer.getFrameIndex(window.name); // 获取当前窗口的索引
                        parent.layer.close(index); // 关闭当前窗口
                    }, 500); // 延迟关闭窗口
                })
                .catch(error => {
                    console.error('Error:', error);
                    layer.msg('提交失败，请重试');
                });
                return false; // 阻止表单默认提交行为
            });
        });
    </script>
</body>
</html>